package cn.com.blueInfo.bpm.template.service.impl;

import cn.com.blueInfo.bpm.template.convert.ParallelBranchConvert;
import cn.com.blueInfo.bpm.template.entity.ParallelBranch;
import cn.com.blueInfo.bpm.template.repository.ParallelBranchRepository;
import cn.com.blueInfo.bpm.template.request.ParallelBranchDTO;
import cn.com.blueInfo.bpm.template.response.ParallelBranchVO;
import cn.com.blueInfo.bpm.template.service.ParallelBranchService;
import cn.com.blueInfo.core.util.SnowflakeIdGenerator;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 并行分支表 业务服务实现类
 * @author suxch
 * @since 2025-09-01 19:30:44
 */
@Log4j2
@Service
public class ParallelBranchServiceImpl implements ParallelBranchService {

    @Autowired
    private ParallelBranchRepository parallelBranchRepository;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @Autowired
    private ParallelBranchConvert parallelBranchConvert;

    /**
     * 新增并行分支
     * @param parallelBranchDTO 并行分支参数
     */
    @Override
    public ResultInfo<Integer> addParallelBranch(ParallelBranchDTO parallelBranchDTO) {
        ParallelBranch parallelBranch = parallelBranchConvert.dtoToEntity(parallelBranchDTO);
        parallelBranch.setId(snowflakeIdGenerator.nextId());
        Integer addSize = parallelBranchRepository.createParallelBranch(parallelBranch);
        return ResultInfo.<Integer>builder().data(addSize).message("并行分支成功新增 {} 条").build();
    }

    /**
     * 批量新增并行分支
     * @param parallelBranchDTOList 并行分支参数列表
     */
    @Override
    public ResultInfo<Integer> batchAddParallelBranch(List<ParallelBranchDTO> parallelBranchDTOList) {
        List<ParallelBranch> parallelBranchList = parallelBranchConvert.dtoToEntity(parallelBranchDTOList);
        for (ParallelBranch parallelBranch : parallelBranchList) parallelBranch.setId(snowflakeIdGenerator.nextId());
        Integer addSize = parallelBranchRepository.batchCreateParallelBranch(parallelBranchList);
        return ResultInfo.<Integer>builder().data(addSize).message("并行分支成功新增 {} 条").build();
    }

    /**
     * 删除并行分支
     * @param parallelBranchId 并行分支ID
     */
    @Override
    public ResultInfo<Integer> removeParallelBranch(Long parallelBranchId) {
        Integer removeSize = parallelBranchRepository.deleteParallelBranch(parallelBranchId);
        return ResultInfo.<Integer>builder().data(removeSize).message("并行分支成功删除 {} 条").build();
    }

    /**
     * 批量删除并行分支
     * @param parallelBranchIdList 并行分支ID列表
     */
    @Override
    public ResultInfo<Integer> batchRemoveParallelBranch(List<Long> parallelBranchIdList) {
        Integer removeSize = parallelBranchRepository.batchDeleteParallelBranch(parallelBranchIdList);
        return ResultInfo.<Integer>builder().data(removeSize).message("并行分支成功删除 {} 条").build();
    }

    /**
     * 更新部分并行分支
     * @param parallelBranchUpdatePartialDTO 并行分支参数
     */
    @Override
    public ResultInfo<Integer> modifyParallelBranchPartial(
            ParallelBranchDTO.UpdatePartialDTO parallelBranchUpdatePartialDTO) {
        ParallelBranch parallelBranch = parallelBranchConvert.dtoToEntity(parallelBranchUpdatePartialDTO);
        Integer modifySize = parallelBranchRepository.updateParallelBranchPartial(parallelBranch);
        return ResultInfo.<Integer>builder().data(modifySize).message("并行分支成功更新 {} 条").build();
    }

    /**
     * 更新全部并行分支
     * @param parallelBranchDTO 并行分支参数
     */
    @Override
    public ResultInfo<Integer> modifyParallelBranch(ParallelBranchDTO parallelBranchDTO) {
        ParallelBranch parallelBranch = parallelBranchConvert.dtoToEntity(parallelBranchDTO);
        Integer modifySize = parallelBranchRepository.updateParallelBranch(parallelBranch);
        return ResultInfo.<Integer>builder().data(modifySize).message("并行分支成功更新 {} 条").build();
    }

    /**
     * 查询一条并行分支
     * @param parallelBranchId 并行分支ID
     */
    @Override
    public ResultInfo<ParallelBranchVO> queryParallelBranchById(Long parallelBranchId) {
        ParallelBranch parallelBranch = parallelBranchRepository.getParallelBranchById(parallelBranchId);
        ParallelBranchVO parallelBranchVO = parallelBranchConvert.entityToVO(parallelBranch);
        return ResultInfo.<ParallelBranchVO>builder().data(parallelBranchVO).message("并行分支查询成功").build();
    }

    /**
     * 分页查询并行分支
     * @param parallelBranchQueryDTO 并行分支参数
     */
    @Override
    public ResultInfo<Page<ParallelBranchVO>> queryParallelBranchPage(
            ParallelBranchDTO.QueryDTO parallelBranchQueryDTO) {
        Page<ParallelBranch> parallelBranchPage =
                parallelBranchRepository.getParallelBranchPage(parallelBranchQueryDTO);
        Page<ParallelBranchVO> resultData = parallelBranchConvert.convertPage(parallelBranchPage);
        return ResultInfo.<Page<ParallelBranchVO>>builder()
                .data(resultData)
                .message("并行分支分页查询成功，共 {} 条", resultData.getRecords().size())
                .build();
    }

    /**
     * 根据条件查询 并行分支 数量
     * @param parallelBranchQueryDTO 并行分支参数
     */
    @Override
    public ResultInfo<Long> queryParallelBranchCount(ParallelBranchDTO.QueryDTO parallelBranchQueryDTO) {
        Long resultCount = parallelBranchRepository.getParallelBranchCount(parallelBranchQueryDTO);
        return ResultInfo.<Long>builder()
                .data(resultCount)
                .message("并行分支数量查询成功，共 {} 条", resultCount)
                .build();
    }

    /**
     * 根据条件查询 并行分支
     *
     * @param parallelBranchQueryDTO 并行分支参数
     */
    @Override
    public ResultInfo<List<ParallelBranchVO>> queryParallelBranchList(
            ParallelBranchDTO.QueryDTO parallelBranchQueryDTO) {
        List<ParallelBranch> parallelBranchList =
                parallelBranchRepository.getParallelBranchList(parallelBranchQueryDTO);
        List<ParallelBranchVO> resultData = parallelBranchConvert.entityListToVoList(parallelBranchList);
        return ResultInfo.<List<ParallelBranchVO>>builder()
                .data(resultData)
                .message("并行分支分页查询成功，共 {} 条", resultData.size())
                .build();
    }
}
